package com.treeman.navi;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class RouteSearchActivity extends Activity
{
	public final static String TAG = "RouteSearchActivity";

	private Context mContext;

	private int mRequestCode;

	// VIEWS

	private EditText mEditTextFrom;

	private EditText mEditTextTo;

	// ATR

	private String mSearchFrom;

	private String mSearchTo;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// View
		setContentView(R.layout.route_search);
		Log.v(MainActivity.APPTAG, "-> SHOW ROUTE SEARCH ");

		mContext = getApplicationContext();

		mEditTextFrom = (EditText) findViewById(R.id.editTextRouteSearchFrom);
		mEditTextTo = (EditText) findViewById(R.id.editTextRouteSearchTo);

		mEditTextFrom.requestFocus();

		try
		{
			// get params
			Bundle bun = this.getIntent().getExtras();

			mRequestCode = bun.getInt("requestCode");

		} catch (Exception ex)
		{
			Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
		}
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if ((keyCode == KeyEvent.KEYCODE_BACK))
		{
			try
			{

				Bundle bundle = new Bundle();

				bundle.putString("info", "route search close");

				Intent i = new Intent();

				i.putExtras(bundle);

				setResult(MainActivity.MENU_ROUTE_SEARCH_RESULT_EXIT, i);

				finish();
			} catch (Exception ex)
			{
				Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
			}
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void onDestroy()
	{
		Log.v(MainActivity.APPTAG, "-> CLOSE ROUTE SEARCH ");
		super.onDestroy();
	}


	public void searchClick(View v)
	{
		TextView tvTemp = (TextView) mEditTextFrom;
		mSearchFrom = tvTemp.getText().toString();
		tvTemp = (TextView) mEditTextTo;
		mSearchTo = tvTemp.getText().toString();

		try
		{
			Geocoder gc = new Geocoder(mContext);
			Bundle bundle = new Bundle();

			if (mSearchFrom.length() > 0 && mSearchTo.length() > 0)
			{
				List<Address> addressList = gc.getFromLocationName(mSearchFrom, 1);

				Address addressFrom = addressList.get(0);

				addressList = gc.getFromLocationName(mSearchTo, 1);

				Address addressTo = addressList.get(0);

				if (addressFrom.hasLatitude() && addressFrom.hasLongitude() && addressTo.hasLatitude() && addressTo.hasLongitude())
				{
					bundle.putDouble("fromLat", addressFrom.getLatitude());
					bundle.putDouble("fromLon", addressFrom.getLongitude());
					bundle.putDouble("toLat", addressTo.getLatitude());
					bundle.putDouble("toLon", addressTo.getLongitude());
				}
			}

			bundle.putString("info", "ROUTE SEARCHING ... ");
			// showToastMessage(mPoi);

			Intent i = new Intent();

			i.putExtras(bundle);

			setResult(MainActivity.MENU_ROUTE_SEARCH_RESULT_SEARCH, i);

			finish();
		} catch (Exception ex)
		{
			Log.e(MainActivity.APPTAG, TAG + " Error : ", ex);
		}
	}
}
